﻿#pragma once
#include "yzrutil.h"
#include "io/File.h"
#include "array/Array.hpp"
#include "io/OutputStream.h"
#include "collection/ArrayList.hpp"
#include "io/RandomAccessFile.h"

namespace yzrilyzr_soundbank{
	EBCLASS(ModelByteBuffer){
	private:
	int64_t len=0;
	ModelByteBuffer * root=this;
	yzrilyzr_io::File * file=nullptr;
	int64_t fileoffset=0;
	std::shared_ptr<yzrilyzr_array::ByteArray> buffer;
	int64_t offset=0;
	ModelByteBuffer(ModelByteBuffer * parent, int64_t beginIndex, int64_t endIndex, bool independent);
	public:
	ModelByteBuffer(std::shared_ptr<yzrilyzr_array::ByteArray> buffer);
	ModelByteBuffer(std::shared_ptr<yzrilyzr_array::ByteArray> buffer, u_index offset, u_index len);
	ModelByteBuffer(yzrilyzr_io::File * file);
	ModelByteBuffer(yzrilyzr_io::File * file, u_index offset, u_index len);
	static void loadAll(yzrilyzr_collection::ArrayList<ModelByteBuffer> &col);
	int64_t capacity();
	void writeTo(yzrilyzr_io::OutputStream & out);
	yzrilyzr_io::InputStream * getInputStream();
	std::shared_ptr<ModelByteBuffer> subbuffer(int64_t beginIndex);
	std::shared_ptr<ModelByteBuffer> subbuffer(int64_t beginIndex, int64_t endIndex);
	std::shared_ptr<ModelByteBuffer> subbuffer(int64_t beginIndex, int64_t endIndex, bool independent);
	yzrilyzr_array::ByteArray * array();
	int64_t arrayOffset();
	ModelByteBuffer * getRoot();
	yzrilyzr_io::File * getFile();
	int64_t getFilePointer();
	void load();
	void unload();
	};
	//ECLASS(RandomFileInputStream, public yzrilyzr_io::InputStream){
	//private:
	//std::shared_ptr<yzrilyzr_io::RandomAccessFile> raf=nullptr;
	//int64_t left;
	//int64_t _mark=0;
	//int64_t markleft=0;
	//ModelByteBuffer * parent;
	//public:
	//RandomFileInputStream();
	//int32_t read() override;
	//int64_t read(yzrilyzr_array::ByteArray & b) override;
	//int64_t read(yzrilyzr_array::ByteArray & b, u_index off, u_index len) override;
	//u_index skip(u_index n) override;
	//u_index available() override;
	//void close() override;
	//void mark(int32_t readlimit) override;
	//void reset() override;
	//bool markSupported() override;
	//};
}
